﻿using AuthenticationAPI.Models.DbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace AuthenticationAPI.Controllers
{
    public class ProjectController : ApiController
    {

        [Route("api/project/{prjId}/reviewerList")]
        [HttpGet]
        public HttpResponseMessage GetProjectReviewerList(int prjId)
        {
            using (var db = new AuthenticationEntities())
            using (var mdb = new MetroProjectsEntities())
            {
                var corpName = mdb.MonitorProjectExtraInfo.Single(p => p.PrjId == prjId).ConstructionCorp;
                var userList = (from p in db.AuthorizedProject.Where(p => p.PrjId == prjId)
                                join q in db.User.Where(p => p.Corporation == corpName && p.Authentication == "Manager")
                                on p.UserName equals q.UserName into gs
                                from g in gs.DefaultIfEmpty()
                                where g != null
                                select new
                                {
                                    realName = g.RealName,
                                    userName = g.UserName,
                                    mail = g.Mail,
                                    phone = g.Phone,
                                    wechatId = g.WechatId
                                }).ToList();
                return Request.CreateResponse(HttpStatusCode.OK, userList);
            }
        }
    }
}
